package com.ruoyi.business.service.impl;

import com.ruoyi.business.constant.CarConstant;
import com.ruoyi.business.domain.BusCar;
import com.ruoyi.business.domain.BusCustomer;
import com.ruoyi.business.mapper.BusCarMapper;
import com.ruoyi.business.mapper.BusCustomerMapper;
import com.ruoyi.business.utils.SnUtil;
import com.ruoyi.business.vo.CheckVo;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.ruoyi.business.mapper.BusRentMapper;
import com.ruoyi.business.domain.BusRent;
import com.ruoyi.business.service.BusRentService;

import java.util.List;

/**
*内容: 
*作者: cy
*/   
    
   
@Service
public class BusRentServiceImpl implements BusRentService{


    @Resource
    private BusRentMapper busRentMapper;

    @Autowired
    private BusCarMapper busCarMapper;

    @Autowired
    private BusCustomerMapper busCustomerMapper;

    @Autowired
    private SnUtil snUtil;


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return busRentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(BusRent record) {
        return busRentMapper.insert(record);
    }

    @Override
    public int insertSelective(BusRent record) {
        return busRentMapper.insertSelective(record);
    }

    @Override
    public BusRent selectByPrimaryKey(Integer id) {
        return busRentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(BusRent record) {
        return busRentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BusRent record) {
        return busRentMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<BusRent> selectRentPage(BusRent busRent, LoginUser loginUser) {
        return busRentMapper.selectRentPage(busRent,loginUser);
    }

    @Override
    public CheckVo checkRentSn(String rentSn) {
        BusRent rent = busRentMapper.selectRentByRentSn(rentSn);
        if (ObjectUtils.isEmpty(rent)){
            throw new ServiceException("出租单号不存在,请重新输入");
        }
        //判断这个出租单状态
        if (rent.getRentStatus().equals(CarConstant.CAR_IS_BACK)){
            throw new ServiceException("该出租订单已经完成,请勿重复检查入库");
        }
        CheckVo checkVo = new CheckVo();
        //1.生成一个检查单号
        String checkSn = snUtil.createSn(CarConstant.CHECK_SN_PREFIX);
        //2.拿到操作员
        String username = SecurityUtils.getUsername();
        //3.根据身份证号查询客户信息
        BusCustomer busCustomer = busCustomerMapper.selectByIdentity(rent.getIdentity());
        //4.根据车牌号查询车辆信息
        BusCar busCar = busCarMapper.selectCarByCarNum(rent.getCarNumber());
        checkVo.setCheckSn(checkSn);
        checkVo.setRentSn(rentSn);
        checkVo.setOpername(username);
        checkVo.setCustomer(busCustomer);
        checkVo.setCar(busCar);
        checkVo.setRent(rent);
        return  checkVo;

    }

}
